package com.forever7776.life.core.service.impl.collect;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.forever7776.life.core.common.util.ResultVoUtil;
import com.forever7776.life.core.entity.collect.CollectBlog;
import com.forever7776.life.core.mapper.collect.CollectBlogMapper;
import com.forever7776.life.core.mapper.stat.StatBlogMapper;
import com.forever7776.life.core.service.collect.ICollectBlogService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.forever7776.life.remote.common.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 博客收藏表 服务实现类
 * </p>
 *
 * @author kz
 * @since 2018-04-11
 */
@Service
public class CollectBlogServiceImpl extends ServiceImpl<CollectBlogMapper, CollectBlog> implements ICollectBlogService {

    @Autowired
    private CollectBlogMapper collectBlogMapper;
    @Autowired
    private StatBlogMapper statBlogMapper;

    /**
     * 查询博客收藏数据
     *
     * @param userId
     * @param blogId
     * @return
     */
    @Override
    public CollectBlog selectCollectOneByUserIdAndBlogId(String userId, Integer blogId) {
        return selectOne(new EntityWrapper<CollectBlog>().
                eq("user_id", userId).eq("blog_id", blogId).eq("status", 0)
        );
    }

    @Override
    public CollectBlog selectNotCollectOneByUserIdAndBlogId(String userId, Integer blogId) {
        return selectOne(new EntityWrapper<CollectBlog>().
                eq("user_id", userId).eq("blog_id", blogId).eq("status", 1));
    }

    /**
     * 给博客添加收藏统计
     *
     * @param userId
     * @param blogId
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultVO addCollectStat(String userId, Integer blogId) {
        if (selectCollectOneByUserIdAndBlogId(userId, blogId) != null) {
            return ResultVoUtil.getFailResult("该博客已经收藏~");
        }
        statBlogMapper.addCollectStatCount(blogId);
        selectOneOrInsert(userId, blogId);
        collectBlogMapper.updateCollectBlogStatusCollect(userId, blogId);
        return ResultVoUtil.getSuccessResult("给博客添加收藏成功~");
    }

    /**
     * 博客取消收藏
     *
     * @param userId
     * @param blogId
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultVO cutCollectStat(String userId, Integer blogId) {
        if (selectNotCollectOneByUserIdAndBlogId(userId, blogId) != null) {
            return ResultVoUtil.getFailResult("该博客已经取消收藏~");
        }
        statBlogMapper.cutCollectStatCount(blogId);
        selectOneOrInsert(userId, blogId);
        collectBlogMapper.updateCollectBlogStatusNotCollect(userId, blogId);
        return ResultVoUtil.getSuccessResult("博客取消收藏成功~");
    }

    /**
     * 查询一条数据，如果没有则插入
     *
     * @param userId
     * @param blogId
     * @return
     */
    @Override
    public CollectBlog selectOneOrInsert(String userId, Integer blogId) {
        CollectBlog detail = selectOne(new EntityWrapper<CollectBlog>().
                eq("user_id", userId).eq("blog_id", blogId));
        if (detail == null) {
            detail = new CollectBlog();
            detail.setUserId(userId);
            detail.setBlogId(blogId);
            collectBlogMapper.insert(detail);
        }
        return detail;
    }
}
